﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BrightIdeasSoftware;
using ROrganizer.Client;
using ROrganizer.UI.Controllers;
using ROrganizer.UI.Forms.Core;
using ROrganizer.UI.Models;

namespace ROrganizer.UI.Forms
{
    internal partial class ItemDetailsForm : Form
    {
        private readonly ItemDetailsFormController controller;
        private readonly GroupManager<ItemDetailsFormController, object> groupper;

        public ItemDetailsForm(ImdbEntry[] entries)
        {
            this.controller = new ItemDetailsFormController(entries);
            this.InitializeComponent();

            this.groupper = new GroupManager<ItemDetailsFormController, object>(this.olvFiles, null);
            this.InitializeGroups();
        }

        private void InitializeGroups()
        {
            this.groupper.SetFormat<Resolution>(this.chResolution, Formatter.FormatResolution);
            this.groupper.SetFormat<long>(this.chSize, Formatter.FormatSize);
            this.groupper.SetFormat<int>(this.chRuntime, Formatter.FormatRuntime);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.olvFiles.Normalize();

            this.tbTitle.Text = this.controller.Title;
            this.tbRating.Text = this.controller.Rating;
            this.tbTagline.Text = this.controller.Tagline;
            this.tbPlot.Text = this.controller.Plot;
            this.tbPublished.Text = this.controller.Published;

            this.lbGenres.Items.AddRange(this.controller.Genres);
            this.lbKeywords.Items.AddRange(this.controller.Keywords);

            this.olvFiles.AddObjects(this.controller.Files);
            this.olvFiles.Sort(this.chFileName, SortOrder.Ascending);
        }

        private void OnDoubleClicked(object sender, MouseEventArgs e)
        {
            if (this.olvFiles.SelectedObject == null)
                return;

            this.ExecuteAndWatch(this.controller.OpenFile, (ItemDetailsFormController.FileEntry)this.olvFiles.SelectedObject);
        }
    }
}
